--This is player.lua

function GM:CanPlayerSuicide( ply )
	if ( ply:Team( ) != TEAM_SPECTATOR && ply:Team( ) != TEAM_CONNECTING && ply:Team( ) != TEAM_UNASSIGNED) then
		return true
	else
		return false
	end
end

function GM:PlayerLoadout( pl )
		pl:StripWeapons( )
		pl:StripAmmo( )
		if (pl:Team( ) == TEAM_COMBINE or pl:Team( ) == TEAM_LAMBDA) then
			pl:Give( "weapon_throwingarm" )
		end
		if (string.find(pl:Nick(), "Bot") != nil and pl:SteamID() == "BOT") then
			pl:GiveAmmo( 5, "grenade")
		end
end

function GM:PlayerSetModel( pl )
	local modelname = ''
	if (string.find(pl:Nick(), "Bot") != nil and pl:SteamID() == "BOT") then
		if ( pl:Team( ) == TEAM_COMBINE or pl:Team( ) == TEAM_DEADCOMB ) then
			modelname = PlayerModelsDir .. table.Random( PlayerModelsCombine )
		elseif ( pl:Team( ) == TEAM_LAMBDA or pl:Team( ) == TEAM_DEADLAMB ) then
			modelname = PlayerModelsDir .. table.Random( PlayerModelsLambda )
		end
	else
		if ( pl:Team( ) == TEAM_COMBINE or pl:Team( ) == TEAM_DEADCOMB ) then
			modelname = PlayerModelsDir .. pl.PrefCombModel
		elseif ( pl:Team( ) == TEAM_LAMBDA or pl:Team( ) == TEAM_DEADLAMB ) then
			modelname = PlayerModelsDir .. pl.PrefLambModel
		end
	end
	util.PrecacheModel( modelname )
	pl:SetModel( modelname )
end

function GM:PlayerInitialSpawn( pl )
	pl.VotedMapIndex = 0
	pl.DeathTime = 0
	--First thing, check if the player is a bot spawned by the console
	--The difference between the bots and normal players is that bots will be forced into choosing a team.
	if (string.find(pl:Nick(), "Bot") != nil and pl:SteamID() == "BOT") then
		pl:SetTeam( TEAM_UNASSIGNED )
		AutoTeam( pl )
	else
		if ( GetConVar("db_dev"):GetInt( ) == 1 and pl:IsUserGroup( "owner" ) == false ) then
			pl:Kick( "The server is currently experiencing issues. Please check back in an hour." )
		end	
		pl:SetTeam( TEAM_UNASSIGNED )
		pl:ConCommand( "gm_showteam" )
		--Set up some player variables
		local Info = Get_Player_Data( pl )
		pl.JoinTime = CurTime( )
		pl.TimeSoFar = tonumber( Info[2] )
		pl.PrefCombModel = Info[3]
		pl.PrefLambModel = Info[4]
		pl.MaxBallCount = 3
		Save_Player_Data( pl )
	end
end

function GM:PlayerSpawn( pl )
	if (  pl:Team( ) == TEAM_SPECTATOR || pl:Team( ) == TEAM_UNASSIGNED ) then
		GAMEMODE:PlayerSpawnAsSpectator( pl )
		return
	end
	pl:UnSpectate( )
	hook.Call( "PlayerLoadout", GAMEMODE, pl )	
	hook.Call( "PlayerSetModel", GAMEMODE, pl )
end

function GM:PlayerDeath( Victim, Inflictor, Attacker )
	explode( Victim )
	Victim.DeathTime = CurTime( ) + 3
	if ( Round_Active( ) == true ) then
	--Switch the player's team if they die during the round.
		if ( Victim:Team( ) == TEAM_COMBINE ) then
			Victim:SetTeam( TEAM_DEADCOMB )
		elseif ( Victim:Team( ) == TEAM_LAMBDA ) then
			Victim:SetTeam( TEAM_DEADLAMB )
		end
	end
	--Spawn all the balls this player had.
	local ammo  = Victim:GetAmmoCount( "Grenade" )
	while ( ammo > 0 ) do
		ammo = ammo - 1
		local ent = ents.Create( "ent_ball_disabled" )
		ent:Spawn( )
		ent:Activate( )
		ent:SetPos(Victim:GetPos())
	end

	if ( Inflictor && Inflictor == Attacker && ( Inflictor:IsPlayer( ) || Inflictor:IsNPC( ) ) ) then
		Inflictor = Inflictor:GetActiveWeapon( )
		if ( !Inflictor || Inflictor == NULL ) then Inflictor = Attacker end
	end

	if ( Attacker == Victim ) then
		umsg.Start( "PlayerKilledSelf" )
			umsg.Entity( Victim )
		umsg.End( )
	return end
	if ( Attacker:IsPlayer( ) ) then
		umsg.Start( "PlayerKilledByPlayer" )
			umsg.Entity( Victim )
			umsg.String( Inflictor:GetClass( ) )
			umsg.Entity( Attacker )
		umsg.End( )
	return end
	umsg.Start( "PlayerKilled" )
		umsg.Entity( Victim )
		umsg.String( Inflictor:GetClass( ) )
		umsg.String( Attacker:GetClass( ) )
	umsg.End( )
end

function GM:PlayerDeathThink( ply )
	--Wait until the death delay has run out.
	if ( CurTime( ) > ply.DeathTime ) then
		ply:Spawn( )
	end
end

function GM:PlayerCanJoinTeam( ply, teamid )
	if ( ply:Team( ) == teamid ) then 
		ply:ChatPrint( "You're already on that team" )
		return false
	end
	return true
end

/*---------------------------------------------------------
   Name: gamemode:PlayerDisconnected( )
   Desc: Player has disconnected from the server.
---------------------------------------------------------*/
function GM:PlayerDisconnected( player )
	Save_Player_Data( player )
end

function GM:PlayerDeathSound()
	return true
end